package nimy.info.android.dinnertime.page;

import info.nimy.google.recipesearch.html.RecipeItem;
import nimy.info.android.dinnertime.R;
import nimy.info.android.dinnertime.utils.AppUtils;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;

/**
 * The loading style like <a href=
 * "http://stackoverflow.com/questions/11619026/webview-with-sherlockactionbar-reloads-on-rotation"
 * >Style</a>
 * 
 * @author 234aini@gmail.com 2012-10-18
 * 
 */
public class WebViewPage extends AbstractSinglePage {
	private static final String TAG = WebViewPage.class.getName();
	private WebView webView;
	private View inflated;
	private ProgressBar progressBar;
	private RecipeItem mSelectedRecipe;

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// Place an action bar item for searching.
		MenuItem item = menu.add("Refresh");
		item.setIcon(android.R.drawable.ic_popup_sync);
		item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				loadPage(mSelectedRecipe.getHyperlink());
				return true;
			}
		});
		inflaterShareAction(menu, inflater);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// We have a menu item to show in action bar.
		setHasOptionsMenu(true);
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		inflated = inflater.inflate(R.layout.recipe_show, container, false);
		progressBar = (ProgressBar) inflated
				.findViewById(R.id.recipe_progressbar);
		webView = (WebView) inflated.findViewById(R.id.recipe_webview);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				progressBar.setProgress(progress);
				if (progress == 100) {
					webView.setVisibility(View.VISIBLE);
					progressBar.setVisibility(View.GONE);
				}
			}
		});
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Handle the error
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		return inflated;
	}

	public void loadRecipeItem(final RecipeItem item) {
		this.mSelectedRecipe = item;
		setShareIntent(createShareIntent());
		openUrl(this.mSelectedRecipe.getHyperlink());
	}

	private void loadPage(final String url) {
		webView.setVisibility(View.GONE);
		progressBar.setVisibility(View.VISIBLE);
		webView.loadUrl(url);
	}

	private void openUrl(final String url) {
		if (url == null || url.trim().equals("")) {
			loadPage("about:blank");
		} else {
			loadPage(url);
		}
	}

	@Override
	protected Intent createShareIntent() {
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		Log.d(TAG, "Send the " + this.mSelectedRecipe.toString()
				+ " to other apps");
		shareIntent.putExtra(
				Intent.EXTRA_TEXT,
				this.mSelectedRecipe.getName() + " "
						+ this.mSelectedRecipe.getHyperlink() + " "
						+ AppUtils.getAppLink());
		shareIntent.setType("text/plain");
		return shareIntent;
	}
}
